﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib.EventHandlers;
using Cspl.AnimalFight.Lib.Data;
using System.Text.RegularExpressions;

namespace Cspl.AnimalFight.Lib
{
    public class ModifyAnimal
    {
        
        public void addAnimal(BattleField.NewAnimal newAnimal, BattleField battleField)
        {            
            Console.WriteLine("input data new animal");
            try
            {                
                switch (newAnimal.type)
                {
                    case Animals.Dog:
                        Dog dog = new Dog();
                        dog.Name = newAnimal.name;
                        dog.Mood = newAnimal.mood;
                        dog.AP = newAnimal.AP;
                        dog.DP = newAnimal.DP;
                        dog.HP = newAnimal.HP;                        
                        battleField[dog.Name] = dog;
                        battleField.ShowDetailAnimals(dog, dog);
                        break;
                    case Animals.Duck:
                        Duck duck = new Duck();
                        duck.Name = newAnimal.name;
                        duck.AP = newAnimal.AP;
                        duck.DP = newAnimal.DP;
                        duck.HP = newAnimal.HP;
                        battleField[duck.Name] = duck;
                        battleField.ShowDetailAnimals(duck, duck);
                        break;
                    case Animals.Snake:
                        Snake snake = new Snake();
                        snake.Name = newAnimal.name;
                        snake.AP = newAnimal.AP;
                        snake.DP = newAnimal.DP;
                        snake.HP = newAnimal.HP;
                        battleField[snake.Name] = snake;
                        battleField.ShowDetailAnimals(snake, snake);
                        break;
                    case Animals.Horse:
                        Horse horse = new Horse();
                        horse.Name = newAnimal.name;
                        horse.AP = newAnimal.AP;
                        horse.DP = newAnimal.DP;
                        horse.HP = newAnimal.HP;
                        battleField[horse.Name] = horse;
                        battleField.ShowDetailAnimals(horse, horse);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }

        }

        private bool IsNumericString(String input)
        {
            bool isNum = false;
            try
            {
                Regex rgx = new Regex("^[0-9]*$");
                if (rgx.IsMatch(input))
                {
                    isNum = true;
                }
            }
            catch (Exception ex)
            {                              
                throw new Exception(ex.Message);
            }
            return isNum;
        }

        public int ConvertStringToInt(String input)
        {
            int result = 0;
            if (IsNumericString(input))
            {
                try
                {
                    result = Convert.ToInt32(input);
                }
                catch (Exception)
                {
                    string exMsg = "Input must be in numeric range between " + Int32.MinValue + " and " + Int32.MaxValue;
                    Console.WriteLine(exMsg);
                    throw new Exception(exMsg);
                }
            }
            else
            {
                string exMsg = "Input must be in numeric range";
                Console.WriteLine(exMsg);
                throw new Exception(exMsg);
            }
            return result;
        }
        public string checkAnimalName(string input)
        {
            string result = "";
            if (Regex.IsMatch(input, "^[a-zA-Z0-9]*$"))
            {
                result = input;
            }
            else
            {
                string exMsg = "Input must be in alphanumeric";
                Console.WriteLine(exMsg);
                throw new Exception(exMsg);
            }
            return result;
        }
    }
}
